package middleware

import (
	"server/app/helper"

	"github.com/gin-gonic/gin"
	"github.com/gookit/color"
)

func ErrorRecover() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if r := recover(); r != nil {
				color.Red.Println(c.Request.URL.Path, r)
				switch r.(type) {
				case gin.H:
					helper.Result(c, 0, 1, r.(string), nil, 200)
				default:
					helper.Result(c, 0, 1, "内部错误", nil, 500)
				}
				c.Abort()
			}
		}()
		c.Next()
	}
}
